﻿using System;
using System.IO;
using System.Xml.Serialization;
using Antix.Mail.Smtp.Impostor.Properties;

namespace Antix.Mail.Smtp.Impostor
{
    /// <summary>
    ///   <para>FileMessageStorage Configuration</para>
    /// </summary>
    public class FileMessageStorageConfiguration : MessageStorageConfiguration<FileMessageStorage>
    {
        /// <summary>
        ///   <para>Create object</para>
        /// </summary>
        public FileMessageStorageConfiguration()
        {
            if (Settings.Default == null) return;

            // set default values from the config file
            DropPath = Settings.Default.FileMessageStorage_DropPath;
            if (string.IsNullOrWhiteSpace(DropPath))
            {
                DropPath = string.Concat(Path.GetTempPath().TrimEnd('\\'), "\\{0}_{1}");
            }
            FileExtension = Settings.Default.FileMessageStorage_FileExtension;
        }

        /// <summary>
        /// </summary>
        [XmlElement]
        public string DropPath { get; set; }

        [XmlElement]
        public string FileExtension { get; set; }

        /// <summary>
        ///   <para>Create storage from this config</para>
        /// </summary>
        public override FileMessageStorage Create(HostConfiguration hostConfig)
        {
            if (hostConfig == null) throw new ArgumentNullException("hostConfig");

            var messageStorage = new FileMessageStorage
            {
                DropPath = string.Format(DropPath, hostConfig.IPAddress, hostConfig.Port),
                FileExtension = FileExtension
            };
            messageStorage.Reset();

            return messageStorage;
        }
    }
}